package com.studentmanage.manage_user.controller;

import com.studentmanage.api.system.UserControllerApi;
import com.studentmanage.framework.domain.common.response.ResponseResult;
import com.studentmanage.framework.domain.common.response.ResponseValue;
import com.studentmanage.framework.domain.system.LoginResult;
import com.studentmanage.framework.domain.system.User;
import com.studentmanage.framework.utils.IPUtil;
import com.studentmanage.manage_user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController implements UserControllerApi{
    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;

    @Override
    @PostMapping("/denglu")
    public LoginResult login(@RequestBody User user) {
        String ipAddress = IPUtil.getIpAddress(request);
        return userService.login(user,ipAddress);
    }

    @Override
    @GetMapping("/list")
    public List<User> getList() {
        return userService.getList();
    }

    @Override
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id") String id) {
        return userService.delete(id);
    }

    @Override
    @PostMapping("/add")
    public ResponseResult add(@RequestBody User user) {
        return userService.add(user);
    }

    @Override
    @PostMapping("/edit")
    public ResponseResult update(@RequestBody User user) {
        return userService.update(user);
    }

    @Override
    @GetMapping("/get/{name}")
    public ResponseValue<User> queryUser(@PathVariable("name") String name) {
        return userService.queryUser(name);
    }

    @Override
    @GetMapping("/sql/download")
    public String downloadFile(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        return userService.downloadFile(request,response);
    }
}
